package cn.mall.user.controller;

import cn.mall.auth.annotation.MyPermission;
import cn.mall.base.constants.BasicMap;
import cn.mall.user.dto.LoginDTO;
import cn.mall.user.dto.RechargeDTO;
import cn.mall.user.dto.RegisterDTO;
import cn.mall.user.service.IUserService;
import cn.mall.user.domain.User;
import cn.mall.user.query.UserQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user" )
@MyPermission(name = "用户管理")
public class UserController {

    @Autowired
    public IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 保存和修改公用的
     * @param user 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @MyPermission(name = "用户保存和修改",desc = "用户保存和修改")
    public AjaxResult addOrUpdate(@RequestBody User user) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            if ( user.getId() != null) {
                ajaxResult = userService.updateU(user);
            } else {
                ajaxResult = userService.insertU(user);
            }
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    @MyPermission(name = "删除用户",desc = "删除用户")
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            userService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            User user =userService.loadById(id);
            return AjaxResult.me().setResultObj(user);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List< User> list = userService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public AjaxResult pageList(@RequestBody UserQuery query) {
        try {
            PageList<User> pageList = userService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 用户注册
     * @param dto
     * @return
     */
    @PostMapping("/register")
    public AjaxResult userRegister(@RequestBody RegisterDTO dto){
        try {
            userService.userRegister(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("用户注册失败" + e.getMessage());
        }
    }

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public AjaxResult loginIn(@RequestBody LoginDTO dto){
        try {
            Map<String, Object> loginUser = userService.loginIn(dto);
            return AjaxResult.me().setResultObj(loginUser);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 登出
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/logout")
    public AjaxResult loginOut(HttpServletRequest httpServletRequest){
        try {
            String token = httpServletRequest.getHeader("token");
            redisTemplate.delete(token);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 用户余额充值
     * @param rechargeDTO
     * @return
     */
    @PostMapping("/recharge")
    @MyPermission(name = "用户余额充值",desc = "用户余额充值")
    public AjaxResult balanceRecharge(@RequestBody RechargeDTO rechargeDTO){
        try {
            userService.balanceRecharge(rechargeDTO);
            return AjaxResult.me().setMessage("充值成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/getBalaceAll")
    public AjaxResult getBalaceAll() {
        try {
            Float balanceAlld  = userService.getBalaceAll();
            return AjaxResult.me().setResultObj(balanceAlld);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }



}
